---- Control mode functions 


function setTargetMode(callbackfn, itemid)

	controlMode = "targetmode"
	targetLoc[1] = characterLoc[1] + playerHeading.value[1]
	targetLoc[2] = characterLoc[2] + playerHeading.value[2]

	usingItem = itemid
	targetCallback = callbackfn

end

function killPlayer(deathcause)
	controlMode = "deadmode"
	addConsoleMessage("You are dead! You " ..deathcause)
end

function handleDeadControl(key,unicode)

if (key == "r") then
  	initGame()
  	return
  end

end

function handleConsoleControl(key,unicode)

	if (key == "escape" or key == "c") then
		controlMode = "movementmode"
		return
	end

end

function handlePanelControl(key,unicode)
	if (key == 'escape') then
		controlMode = "movementmode"
		return
	end
end

function handleMoveControl(key,unicode)

  if key == 'up' then
      gameShift(playerHeading.value[1],playerHeading.value[2]) 
   elseif key == 'down' then
      gameShift(-playerHeading.value[1],-playerHeading.value[2]) 
      elseif key == 'left' then
      playerHeading = playerHeading.next 
      elseif key == 'right' then
      playerHeading = playerHeading.prev 
  end

  --  Restart (tmp) - end of game
  if (getLevel() >= 4 or love.keyboard.isDown("lshift") or love.keyboard.isDown("rshift")) then
	if (key == "r") then
	  	initGame()
	  	return
	  end
	end

	 -- Expanded console
	 if (key == "c") then
	 	controlMode = "consolemode"
	 	return
	 end

  --- Item management ____________________________________________
  if (key == "g") then
  	if (isItemOnGround(characterLoc[1],characterLoc[2]) == false) then
  		addConsoleMessage("You see no items here")
  	else 
  		targetItem = nil
  		controlMode = "pickup"
  	end
  end

  if (key == "d") then
  	controlMode = "dropmode"
  	return
  end

  if (key == "l") then
  	setTargetMode(lookCallback)
  	addConsoleMessage("What do you want to look at?")
  	targetCallback = lookCallback
  	return
  end


  -- aparrently, lua is fussy about casting 'key' to the array index?
	if	(key == "1") then
			useItem(1)
	   elseif
	   		(key == "2") then
		useItem(2)
		elseif
	   		(key == "3") then
		useItem(3)
		elseif
	   		(key == "4") then
		useItem(4)
		elseif
	   		(key == "5") then
		useItem(5)
		elseif
	   		(key == "6") then
		useItem(6)
		elseif
	   		(key == "7") then
		useItem(7)
		elseif
	   		(key == "8") then
		useItem(8)
		elseif
	   		(key == "9") then
		useItem(9)
	end

	-- waitkey
	if (key == "w") then
		gameTick()
	end

	return
end


function handleTargetControl(key,unicode)
	if key == 'up' then
	      targetLoc[2] = targetLoc[2] - 1 
	   elseif key == 'down' then
	      targetLoc[2] = targetLoc[2] + 1 
	      elseif key == 'left' then
	      targetLoc[1] = targetLoc[1] - 1 
	      elseif key == 'right' then
	      targetLoc[1] = targetLoc[1] + 1 
	end

  if (targetLoc[1] < 1) then targetLoc[1] = 1 end
  if (targetLoc[2] < 1) then targetLoc[2] = 1 end

  if (targetLoc[1] > worldData.size.x) then targetLoc[1] = worldData.size.x end
  if (targetLoc[2] > worldData.size.y) then targetLoc[2] = worldData.size.y end

  if (key == 'return' or key == ' ') then

	  	-- Kill callback if out of range
	  	if (localTerrain[targetLoc[1]][targetLoc[2]].FOV == false and playerItems[usingItem].itemtype ~= "scanner") then
	  		addConsoleMessage("You cannot see there.")
	  		targetCallback = nil
	  	end

	  	if (targetCallback ~= nil ) then
	  		targetCallback(targetLoc)
	  		targetCallback = nil
	  	end

	  	controlMode = "movementmode"
	end

	if (key == 'escape') then
	  	addConsoleMessage("You cancel your action")
	  	targetCallback = nil
	  	controlMode = "movementmode"
	end

end


function handlePickupControl(key,unicode)


	if (key == 'escape') then
  		controlMode = "movementmode"
  		targetItem = nil
  	end

  	for i,v in ipairs(letterMap) do
  		if (key == v) then
  			if (localTerrain[characterLoc[1]][characterLoc[2]].items[i].itemtype ~= "empty") then
  				targetItem = i
  			else
  				targetItem = nil
  			end
  		 break
  		 end
  	end

  	-- attempt pick up
  	if (targetItem ~= nil) then

  		local success = false

  		if	(key == "1") then
			success =  attemptPickUp(targetItem, 1)
	   	elseif (key == "2") then
			success =  attemptPickUp(targetItem, 2)
		elseif (key == "3") then
			success =  attemptPickUp(targetItem, 3)
		elseif (key == "4") then
			success =  attemptPickUp(targetItem, 4)
		elseif (key == "5") then
			success =  attemptPickUp(targetItem, 5)
		elseif (key == "6") then
			success =  attemptPickUp(targetItem, 6)
		elseif (key == "7") then
			success =  attemptPickUp(targetItem, 7)
		elseif (key == "8") then
			success =  attemptPickUp(targetItem, 8)
		elseif (key == "9") then
			success =  attemptPickUp(targetItem, 9)
		end

		if (success == true) then
			targetItem = nil
			controlMode = "movementmode"
			gameTick()
		end

  	end
end


function handleDropControl(key,unicode)

	if (key == 'escape') then
  		controlMode = "movementmode"
  		targetItem = nil
 	end

  	local success = false

  	if	(key == "1") then
			success =  attemptDrop(1)
	   	elseif (key == "2") then
			success =  attemptDrop(2)
		elseif (key == "3") then
			success =  attemptDrop(3)
		elseif (key == "4") then
			success =  attemptDrop(4)
		elseif (key == "5") then
			success =  attemptDrop(5)
		elseif (key == "6") then
			success =  attemptDrop(6)
		elseif (key == "7") then
			success =  attemptDrop(7)
		elseif (key == "8") then
			success =  attemptDrop(8)
		elseif (key == "9") then
			success =  attemptDrop(9)
	end

	if (success == true) then 
		controlMode = "movementmode" 
		gameTick()
	end
	
end




